﻿#if ODIN_INSPECTOR
using Sirenix.OdinInspector;
#endif
using TMPro;
using UnityEngine;

namespace JH
{
    /// <summary>
    ///  点信息文本组件，用于显示鼠标悬浮时的点信息
    /// </summary>
    public class PointInfoText : MonoBehaviour, IMouseHoverSample
    {
        /// <summary>
        ///  点信息文本组件，用于显示鼠标悬浮时的点信息
        /// </summary>
        [SerializeField] private TextMeshProUGUI pointInfoText;

        /// <summary>
        /// 单位
        /// </summary>
        [SerializeField]
#if ODIN_INSPECTOR
        [LabelText("单位")]
#endif
        public string unit;

        /// <summary>
        ///  格式
        /// </summary>
        [SerializeField]
#if ODIN_INSPECTOR
        [LabelText("格式")]
#endif
        private string format = "0.##";

        /// <summary>
        ///  当鼠标悬浮状态改变时调用
        /// </summary>
        public void OnHoverStateChanged(bool newHoverState)
        {
            if (pointInfoText != null)
            {
                pointInfoText.gameObject.SetActive(newHoverState);
            }
        }

        public void OnSample(LineVertexInfo sample)
        {
            if (!Application.isPlaying) return;

            // 如果点信息文本未设置，直接返回
            if (pointInfoText == null) return;

            // 设置点信息文本内容
             pointInfoText.text = sample.OriginPoint.y.ToString(format) + " " + unit;
        }

        private void OnValidate()
        {
            // 确保点信息文本组件在编辑器中可见
            if (pointInfoText == null)
                pointInfoText = GetComponentInChildren<TextMeshProUGUI>(true);
        }
    }
}